num genre
1 125 Horror
2 110 Thrillers
3 38 Comedies
4 19 SciFi&Fantasy
5 12 Cult
6 2 Documentaties
7 2 Romantic
December 9, 2024
CI <- function(data, coverage_prob){
#Generates a normal prediction interval with an intended coverage probability of coverage_prob based on a vector of numeric data
lower_zscore <- qnorm((1-coverage_prob)/2)
upper_zscore <- qnorm(((1-coverage_prob)/2) + coverage_prob)
avg <- mean(data)
stan_d <- sd(data)
lower_bound <- avg + lower_zscore*stan_d
upper_bound <- avg + upper_zscore*stan_d
return(data.frame(PI_percentage = coverage_prob, lower = lower_bound, upper = upper_bound))
}one_beta_simulation <- function(n, alpha, beta, ci_prop){
#Assesses prediction accuracy and actual coverage probability of a normal prediction interval when used on a vector of numeric data of size n. The numeric data is generated from a beta distribution with parameters alpha and beta.
cover_df <- CI(rbeta(n, alpha, beta), ci_prop)
cover_prop <- pbeta(cover_df[1, "upper"], alpha, beta) - pbeta(cover_df[1, "lower"], alpha, beta)
mean_in_interval <- .5 >= cover_df[1, "lower"] & .5 <= cover_df[1,"upper"]
param_df <- data.frame(cover = cover_prop, alpha = rep(alpha, nrow(cover_df)), beta = rep(beta, nrow(cover_df)), mean_in_interval = mean_in_interval)
df <- cbind(cover_df, param_df)
return(df)
}beta_sims_n <- function(n){
#Iterates over a vector of possible alpha = beta values and applies one_beta_simulation to each possible value of alpha/beta. All simulations use data of sample size n.
df1 <- map(parameters,\(param) one_beta_simulation(n, param, param, ci) ) %>%
list_rbind()
df2 <- data.frame(n = rep(n, nrow(df1)))
df <- cbind(df2, df1)
return(df)
} n PI_percentage lower upper cover alpha beta mean_in_interval
1 320 0.95 0.3222876 0.6784408 0.9545730 15 15 TRUE
2 107 0.95 0.3597021 0.6426316 0.9578534 25 25 TRUE
3 345 0.95 0.4328748 0.5650138 0.9474185 107 107 TRUE
4 354 0.95 0.4211385 0.5844857 0.9420430 67 67 TRUE
5 427 0.95 0.4429846 0.5591384 0.9539957 147 147 TRUE
6 354 0.95 0.2400498 0.7524044 0.9574436 7 7 TRUE
7 301 0.95 0.4461885 0.5571148 0.9429824 147 147 TRUE
8 24 0.95 0.3128630 0.7243206 0.9243774 9 9 TRUE
9 125 0.95 0.4278135 0.5756722 0.9667129 103 103 TRUE
10 19 0.95 0.4366048 0.5975455 0.9352990 77 77 TRUE